using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace MicroMsg.Storage
{
	[DataContract]
	public class SnsRegionInfo
	{
		[DataMember]
		public List<SnsRegion> regions = new List<SnsRegion>();
		public ulong getFirst()
		{
			if (this.regions.get_Count() <= 0)
			{
				return 0uL;
			}
			if (this.regions.get_Item(0).list_0.get_Count() <= 0)
			{
				return 0uL;
			}
			return this.regions.get_Item(0).list_0.get_Item(0);
		}
		public List<ulong> getList()
		{
			List<ulong> list = new List<ulong>();
			if (this.regions.get_Count() <= 0)
			{
				return list;
			}
			using (List<SnsRegion>.Enumerator enumerator = this.regions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsRegion current = enumerator.get_Current();
					list.AddRange(current.list_0);
				}
			}
			return list;
		}
		private bool deleteFirst(List<ulong> pageList)
		{
			if (this.regions.get_Count() <= 0)
			{
				return false;
			}
			SnsRegion snsRegion = this.regions.get_Item(0);
			if (Enumerable.Count<ulong>(snsRegion.list_0) <= 0)
			{
				this.regions.RemoveAt(0);
				return true;
			}
			if (Enumerable.First<ulong>(pageList) >= Enumerable.First<ulong>(snsRegion.list_0) && Enumerable.Last<ulong>(pageList) <= Enumerable.Last<ulong>(snsRegion.list_0))
			{
				this.regions.RemoveAt(0);
				return true;
			}
			if (Enumerable.First<ulong>(snsRegion.list_0) > Enumerable.First<ulong>(pageList))
			{
				this.regions.RemoveAt(0);
				return true;
			}
			return false;
		}
		private void addFirst(List<ulong> pageList, uint requestTime)
		{
			this.regions.Insert(0, new SnsRegion(pageList, requestTime));
		}
		private void joinFirst(List<ulong> pageList, uint requestTime)
		{
			SnsRegion snsRegion = this.regions.get_Item(0);
			ulong num = Enumerable.Last<ulong>(pageList);
			int num2 = 0;
			using (List<ulong>.Enumerator enumerator = snsRegion.list_0.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ulong current = enumerator.get_Current();
					if (current < num)
					{
						break;
					}
					num2++;
				}
			}
			if (num2 <= 0)
			{
				Log.smethod_1("SnsPage", "SnsObjIDRegionList joinFirst error ");
				return;
			}
			snsRegion.removeTop(num2);
			snsRegion.list_0.InsertRange(0, pageList);
			snsRegion.times.Insert(0, requestTime);
			snsRegion.times.Insert(0, (uint)pageList.get_Count());
		}
		public void replaceFirstRegion(List<ulong> pageList, uint requestTime)
		{
			if (pageList == null || pageList.get_Count() <= 0 || requestTime == 0u)
			{
				return;
			}
			while (this.deleteFirst(pageList))
			{
			}
			if (this.regions.get_Count() <= 0)
			{
				this.addFirst(pageList, requestTime);
				return;
			}
			SnsRegion snsRegion = this.regions.get_Item(0);
			if (Enumerable.Last<ulong>(pageList) > Enumerable.First<ulong>(snsRegion.list_0))
			{
				this.addFirst(pageList, requestTime);
				return;
			}
			this.joinFirst(pageList, requestTime);
		}
		private SnsRegion getRegion(ulong ulong_0)
		{
			SnsRegion result;
			using (List<SnsRegion>.Enumerator enumerator = this.regions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsRegion current = enumerator.get_Current();
					if (current.list_0.Contains(ulong_0))
					{
						result = current;
						return result;
					}
				}
				goto IL_44;
			}
			return result;
			IL_44:
			return null;
		}
		public SnsRegion getNextRegion(ulong id_max, int int_0)
		{
			if (this.regions.get_Count() <= 0)
			{
				return null;
			}
			SnsRegion snsRegion = this.regions.get_Item(0);
			int num = snsRegion.list_0.IndexOf(id_max);
			if (num >= 0)
			{
				if (id_max != Enumerable.Last<ulong>(snsRegion.list_0))
				{
					num++;
					SnsRegion snsRegion2 = new SnsRegion();
					snsRegion2.list_0.AddRange(Enumerable.Take<ulong>(Enumerable.Skip<ulong>(snsRegion.list_0, num), int_0));
					snsRegion2.times.Add((uint)snsRegion2.list_0.get_Count());
					snsRegion2.times.Add(snsRegion.getTimeMin(num, snsRegion2.list_0.get_Count()));
					return snsRegion2;
				}
			}
			return null;
		}
		public void breakTailAt(ulong maxID)
		{
			int i = 0;
			while (i < this.regions.get_Count())
			{
				if (Enumerable.Last<ulong>(this.regions.get_Item(i).list_0) >= maxID)
				{
					i++;
				}
				else
				{
					this.regions.get_Item(i).breakTailAt(maxID);
					i++;
					if (i < this.regions.get_Count())
					{
						this.regions.RemoveRange(i, this.regions.get_Count() - i);
						return;
					}
					return;
				}
			}
		}
		public void removeRange(List<ulong> del_ids)
		{
			bool flag = false;
			if (del_ids != null && del_ids.get_Count() > 0)
			{
				using (List<SnsRegion>.Enumerator enumerator = this.regions.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsRegion current = enumerator.get_Current();
						List<ulong> list = Enumerable.ToList<ulong>(Enumerable.Intersect<ulong>(current.list_0, del_ids));
						if (list.get_Count() > 0)
						{
							using (List<ulong>.Enumerator enumerator2 = list.GetEnumerator())
							{
								while (enumerator2.MoveNext())
								{
									ulong current2 = enumerator2.get_Current();
									current.remove(current2);
								}
							}
							if (current.isEmpty())
							{
								flag = true;
							}
						}
					}
				}
				if (flag)
				{
					List<SnsRegion> list2 = Enumerable.ToList<SnsRegion>(Enumerable.Where<SnsRegion>(this.regions, (SnsRegion snsRegion_0) => snsRegion_0.isEmpty()));
					this.regions = Enumerable.ToList<SnsRegion>(Enumerable.Except<SnsRegion>(this.regions, list2));
				}
				return;
			}
		}
		public void reset(List<ulong> pageList, uint requestTime)
		{
			this.regions.Clear();
			if (pageList.get_Count() > 0)
			{
				this.regions.Add(new SnsRegion(pageList, requestTime));
			}
		}
		public void dump()
		{
			using (List<SnsRegion>.Enumerator enumerator = this.regions.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsRegion current = enumerator.get_Current();
					current.dump();
				}
			}
		}
		public static void dump(List<ulong> list_0)
		{
			string text = "id=";
			using (List<ulong>.Enumerator enumerator = list_0.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ulong current = enumerator.get_Current();
					text = text + current + ",";
				}
			}
		}
		public static void test()
		{
			List<ulong> list = new List<ulong>();
			list.Add(9uL);
			list.Add(8uL);
			list.Add(7uL);
			List<ulong> pageList = list;
			List<ulong> list2 = new List<ulong>();
			list2.Add(10uL);
			list2.Add(9uL);
			list2.Add(8uL);
			List<ulong> list3 = list2;
			List<ulong> list4 = new List<ulong>();
			list4.Add(13uL);
			list4.Add(12uL);
			list4.Add(11uL);
			list4.Add(10uL);
			List<ulong> pageList2 = list4;
			List<ulong> list5 = new List<ulong>();
			list5.Add(15uL);
			list5.Add(14uL);
			list5.Add(13uL);
			list5.Add(11uL);
			List<ulong> pageList3 = list5;
			List<ulong> list6 = new List<ulong>();
			list6.Add(20uL);
			list6.Add(10uL);
			list6.Add(18uL);
			SnsRegionInfo snsRegionInfo = new SnsRegionInfo();
			snsRegionInfo.replaceFirstRegion(pageList, 1u);
			snsRegionInfo.dump();
			snsRegionInfo.replaceFirstRegion(list3, 2u);
			snsRegionInfo.dump();
			snsRegionInfo.removeRange(list3);
			snsRegionInfo.dump();
			snsRegionInfo.replaceFirstRegion(pageList2, 3u);
			snsRegionInfo.dump();
			snsRegionInfo.replaceFirstRegion(pageList3, 4u);
			snsRegionInfo.dump();
			List<ulong> list7 = new List<ulong>();
			list7.Add(13uL);
			list7.Add(12uL);
			List<ulong> del_ids = list7;
			snsRegionInfo.removeRange(del_ids);
			snsRegionInfo.dump();
			SnsRegion nextRegion = snsRegionInfo.getNextRegion(10uL, 4);
			nextRegion.dump();
		}
	}
}
